package com.yonyou.crm.csns.phonebook.appctrl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.yonyou.crm.common.rest.util.RequestUtil;
import com.yonyou.crm.common.rest.util.ResponseUtil;
import com.yonyou.crm.csns.phonebook.entity.PersonListVO;
import com.yonyou.crm.csns.phonebook.rmitf.IPhonebookRmService;

@RestController
@RequestMapping(value="/{version}/mcsns/phonebooks")
public class AppPhonebookCtrl {

	@Autowired
	private IPhonebookRmService service;
	
	@RequestMapping(value = "persons", method = RequestMethod.GET)
	public Object getPersonlist(@PathVariable("version") String version, HttpServletRequest request, HttpServletResponse response) {
		
		JSONObject paramObject = RequestUtil.getParamObject(request);
		Map<String, Object> paraMap = new HashMap<String, Object>();
		String searchKey = (String)paramObject.get("condition");
		paraMap.put("searchKey", searchKey);
		
		List<PersonListVO> list = service.getPersonList(paraMap);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("personlist", list);
		return ResponseUtil.getObject(map);
	}
	
	@RequestMapping(value = "organizations", method = RequestMethod.GET)
	public Object getOrganization(@PathVariable("version") String version, HttpServletRequest request, HttpServletResponse response){
		
		JSONObject paramObject = RequestUtil.getParamObject(request);
		String deptIdStr = (String)paramObject.get("departmentid");
		Long deptId = null;
		if (deptIdStr != null && !deptIdStr.isEmpty()) {
			deptId = Long.parseLong(deptIdStr);
		}
		
		Map<String, Object> map = service.getOrganization(deptId);
		
		return ResponseUtil.getObject(map);
	}
	
	@RequestMapping(value = "count", method = RequestMethod.GET)
	public Object getCount(@PathVariable("version") String version, HttpServletRequest request, HttpServletResponse response){
		
		
		Map<String, Integer> map = service.countOneByDeptIds();
		
		return ResponseUtil.getObject(map);
	}
}
